<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="类别" prop="category">
        <el-input v-model="dataForm.category" placeholder="类别"></el-input>
      </el-form-item>
      <el-form-item label="规格等级" prop="specifications">
        <el-input v-model="dataForm.specifications" placeholder="规格等级"></el-input>
      </el-form-item>
       <el-form-item label="计量单位" prop="unit">
        <el-input v-model="dataForm.unit" placeholder="计量单位"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="价格"></el-input>
      </el-form-item>
      <el-form-item label="指标解释" prop="explain">
        <el-input v-model="dataForm.explain" placeholder="指标解释"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="dataForm.remarks" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="时间" prop="date">
        <el-input v-model="dataForm.date" placeholder="时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        category: "",
        specifications: "",
        unit: "",
        price: "",
       explain: "",
        remarks: "",
        date:""
      },
      dataRule: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        category: [{ required: true, message: "类别不能为空", trigger: "blur" }],
      specifications: [
          { required: true, message: "规格等级不能为空", trigger: "blur" }
        ],
       unit: [{ required: true, message: "计量单位不能为空", trigger: "blur" }],
       price: [{ required: true, message: "价格不能为空", trigger: "blur" }],
        explain: [{ required: true, message: "指标解释不能为空", trigger: "blur" }],
        remarks: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        date: [{ required: true, message: "时间不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/huinong/agriculturalProduction/delete`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.supply.name;
              this.dataForm.category = data.supply.category;
              this.dataForm.phone= data.supply.specifications; 
              this.dataForm.unit= data.supply.unit; 
              this.dataForm.price = data.supply.price;
              this.dataForm.explain = data.supply.explain;
              this.dataForm.remarks = data.supply.remarks;
               this.dataForm.date = data.supply.date;

            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/huinong/agriculturalProduction/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
             category: this.dataForm.category,
              specifications: this.dataForm.specifications, 
              unit: this.dataForm.unit, 
              price:this.dataForm.price,
              explain:this.dataForm.explain,
              remarks:this.dataForm.remarks,
              date:this.dataForm.date,
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
